<?php

class UtilAction extends Action
{
    public function _initialize()
    {
        checkLoginSession();
    }
    
    public function index()
    {
        $this->display('Util:index');
        
    }
    //用户注册提交的方法url
    public function BarcodeInit()
    {
       $this->display('Util:showBar'); 
    }

    public function _init()
    {
     $pdtinfoPdo = D('Product_all_info');
     $bclstPdo = D('Barcode_ptlist');    
     $pdtlist = $pdtinfoPdo->where("real_count > 0")->findAll();
     $cnt =count($pdtlist);  
     if($cnt>0)
     {  $sqlstr = "INSERT INTO `tb_barcode_ptlist` (`simple_code`,`product_name`,`pic`) VALUES ('00000','元宝条码打印','s_logo.gif')";
      for($i=0;$i<$cnt;$i++)
      {
        $spcode = $pdtlist[$i]['simple_code'];        
        $pic = $pdtlist[$i]['pic'];
        $pname = $pdtlist[$i]['product_name'];
        $pic = 's_'.$pic;
        $pcount = $pdtlist[$i]['real_count'];
        while($pcount>0)
        {
        $sqlstr = $sqlstr.",('$spcode','$pname','$pic')";
        $pcount =$pcount -1;    
        }        
      }  
      $clearstr ="TRUNCATE TABLE `tb_barcode_ptlist` ";
      $bclstPdo->execute($clearstr);
      $inrt = $bclstPdo->execute($sqlstr);
      if(($inrt!='') and ($inrt!=null))
      {
        echo ajaxrt('ok','初始化成功');        
      }
      else
      {
        echo ajaxrt('error','初始化有错');
      }
     }
     else
     {
        echo ajaxrt('error','没有库存商品');
     }
       
    }
    //清空barcode表记录
    public function BarcodeClear()
    {

    }
    //清空数据库，初始化库存范例记录
    public function  InitData(){
        $this->display('Util:initData');
    }
    
    public function  _InitData(){
    $pdoMd = M();
    $file_name = COMMON_PATH."initSql.sql";
    header('content-type;text/html;charset=utf-8');
    $fp = @fopen($file_name, "r") or die("不能打开SQL文件 $file_name");//打开文件 
     
    echo "正在执行导入操作...   "."<img id='ajxgif' src = 'PUBLIC/Js/Ajax/ajaxloading.gif' />"; 
    

    while ($line = @fgets($fp, 40960)) 
    {    
        $line = trim($line);
        $exrt = $pdoMd->execute($line);
        //$line = iconv('iso-8859-1','utf-8',$line);
        //echo utf8_encode($line.'<br/>');
        //mb_convert_encoding("妳係我的友仔", "UTF-8", "GBK"); 
        //echo $line.'<br/>';
        
    }   
    echo "<script>$('ajxgif').src ='PUBLIC/Js/Ajax/ajaxloaded.gif';</script>";
    echo "<br/>导入完成";       
    fclose($fp) or die("Can't close file $file_name");//关闭文件   
    }
}

?>